package com.system.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.system.entity.OrganizeInfo;
import com.system.util.CloseResourceUtil;
import com.system.util.GetConnectionUtils;

public class OrganizeDaoImpl implements IOrganizeDao {

	public String addOrganizeInfo(OrganizeInfo organizeInfo) throws SQLException {
		Connection connection = GetConnectionUtils.getoneConnection();
		String sql = "INSERT INTO organize_info VALUES(?,?,?,?)";
		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		preparedStatement.setString(1, organizeInfo.getInfoNum());
		preparedStatement.setString(2, organizeInfo.getInfoName());
		preparedStatement.setString(3, organizeInfo.getInfoContent());
		preparedStatement.setString(4, "");
		if (preparedStatement.executeUpdate() > 0) {
			CloseResourceUtil.close(connection, preparedStatement, null);
			return "1";
		} else {
			CloseResourceUtil.close(connection, preparedStatement, null);
			return "0";
		}
	}

	public String deleteOrganizeInfo(String infoNum) throws SQLException {
		Connection connection = GetConnectionUtils.getoneConnection();
		String sql = "DELETE FROM organize_info WHERE info_num = ?";
		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		preparedStatement.setString(1, infoNum);
		if (preparedStatement.executeUpdate() > 0) {
			CloseResourceUtil.close(connection, preparedStatement, null);
			return "1";
		} else {
			CloseResourceUtil.close(connection, preparedStatement, null);
			return "0";
		}
	}

	public String changeOrganizeInfo(OrganizeInfo organizeInfo, String oldID) throws SQLException {
		Connection connection = GetConnectionUtils.getoneConnection();
		String sql = "UPDATE organize_info SET info_name = ?, info_content= ? WHERE info_num = ?";
		PreparedStatement preparedStatement = connection.prepareStatement(sql);
//		preparedStatement.setString(1, organizeInfo.getInfoNum());
		preparedStatement.setString(1, organizeInfo.getInfoName());
		preparedStatement.setString(2, organizeInfo.getInfoContent());
		preparedStatement.setString(3, oldID);
		if (preparedStatement.executeUpdate() > 0) {
			CloseResourceUtil.close(connection, preparedStatement, null);
			return "1";
		} else {
			CloseResourceUtil.close(connection, preparedStatement, null);
			return "0";
		}
	}

	public List<OrganizeInfo> organizeInfoList(String keyword) throws SQLException {
		Connection connection = GetConnectionUtils.getoneConnection();
		String sql = "SELECT * FROM organize_info WHERE info_name LIKE ?";
		List<OrganizeInfo> list = new ArrayList<OrganizeInfo>();
		if (keyword == null) {

			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "%%");
			ResultSet result = preparedStatement.executeQuery();

			while (result.next()) {
				list.add(new OrganizeInfo(result.getString("info_num"), result.getString("info_name"), result.getString("info_content")));
			}
			CloseResourceUtil.close(connection, preparedStatement, result);
			return list;
		} else {
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "%" + keyword + "%");
			ResultSet result = preparedStatement.executeQuery();

			while (result.next()) {
				list.add(new OrganizeInfo(result.getString("info_num"), result.getString("info_name"), result.getString("info_content")));
			}
			CloseResourceUtil.close(connection, preparedStatement, result);
			return list;
		}
	}

}
